package sopia.sg;

import sopia.sg.R;
import sopia.sg.R.id;
import sopia.sg.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Activity2 extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity2_layout);

		final EditText receiveValueEdit = (EditText) findViewById(R.id.value_receive);
		final Button callReceiverButton = (Button) findViewById(R.id.call_button);
		final Button backButton = (Button) findViewById(R.id.back_button);
		// Lấy về Bundle được gửi kèm Intent rồi lấy ra giá trị
		Bundle receiveBundle = this.getIntent().getExtras();
		final long receiveValue = receiveBundle.getLong("value");

		receiveValueEdit.setText(String.valueOf(receiveValue));

		callReceiverButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Khởi tạo 1 Intent để gửi tới BroadCast Receiver
				// Gắn giá trị vào Intent, lần này ko cần Bundle nữa
				Intent i = new Intent(Activity2.this, Receiver.class);
				i.putExtra("new value", receiveValue - 10);
				sendBroadcast(i);
			}
		});
		backButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Activity2.this, Activity1.class);
				Bundle sendBundle = new Bundle();
				sendBundle.putLong("value", receiveValue);
				i.putExtras(sendBundle);
				startActivity(i);
			}
		});
	}
}
